//
//  WTPersonInfoView.m
//  WTPlayDemo
//
//  Created by ben on 16/7/22.
//  Copyright © 2016年 ben. All rights reserved.
//

#import "WTPersonInfoView.h"
#import "Masonry.h"
#import "WTCategories.h"
#import "WTUserInfoService.h"
#import "UIImage+WTExtend.h"

@implementation WTPersonInfoNickView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setup];
    }
    return self;
}

#pragma mark - 
- (void)setup{
    
    self.nickLabel = ({
        UILabel* label = [[UILabel alloc] init];
        label.textColor = [UIColor hexChangeFloat:@"323232"];
        label.font = [UIFont systemFontOfSize:12];
        label.text = @"昵称:";
        [self addSubview:label];
        label;
    });
    
    self.sexImageView = ({
        UIImageView* view = [[UIImageView alloc] init];
        view.image = [UIImage wt_imageWithName:@"wt_person_sex_women_icon"];
        [self addSubview:view];
        view;
    });

    self.levelImageView = ({
        UIImageView* view = [[UIImageView alloc] init];
        view.image = [UIImage wt_imageWithName:@"wt_carrots_icon"];
        [self addSubview:view];
        view;
    });
   
    
    self.levelLabel = ({
        UILabel* label = [[UILabel alloc] init];
        label.textColor = [UIColor hexChangeFloat:@"323232"];
        label.font = [UIFont systemFontOfSize:8];
        label.text = @"12";
        label.textAlignment = NSTextAlignmentCenter;
        [self.levelImageView addSubview:label];
        label;
    });
    [self layout];
}

#pragma mark - 
- (void)layout{
 
    [self.nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(0);
        make.top.and.bottom.mas_equalTo(0);
    }];
    
    [self.sexImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.mas_equalTo(13);
        make.leading.mas_equalTo(self.nickLabel.mas_trailing).offset(8);
        make.top.mas_equalTo(self.nickLabel.mas_top);
    }];
    
    [self.levelImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(23);
        make.width.mas_equalTo(16);
        make.leading.mas_equalTo(self.sexImageView.mas_trailing).offset(8);
        make.top.mas_equalTo(self.nickLabel.mas_top).offset(-5);
        
    }];
    
    [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.levelImageView.mas_centerX);
        make.centerY.mas_equalTo(self.levelImageView.mas_centerY).offset(3);
    }];
    
}
@end


@implementation WTAddressView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setup];
    }
    return self;
}


#pragma mark - 
- (void)setup{
    
    self.addressLabel = ({
        UILabel* label = [[UILabel alloc] init];
        label.textColor = [UIColor hexChangeFloat:@"969696"];
        label.font = [UIFont systemFontOfSize:9];
        label.text = @"火星";
        [self addSubview:label];
        label;
    });
    
    self.iconImageView = ({
        UIImageView* view = [[UIImageView alloc] init];
        view.image = [UIImage wt_imageWithName:@"wt_person_location_icon"];
        [self addSubview:view];
        view;
    });
    
    [self layout];
}

#pragma mark - 
- (void)layout{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.mas_equalTo(10);
        make.leading.mas_equalTo(0);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(4);
        make.top.and.bottom.mas_equalTo(0);
    }];
}

@end




@interface WTPersonInfoView ()
@property (nonatomic, strong) UIImageView*  iconImageView;
@property (nonatomic, strong) UIView*       contentView;
@property (nonatomic, strong) WTPersonInfoNickView* personInfoNickView;
@property (nonatomic, strong) UILabel*      idLabel;
@property (nonatomic, strong) WTAddressView*    addressView;
@property (nonatomic, strong) UILabel*          desLabel;
@property (nonatomic, strong) WTPersonSocialView*   socialView;

@property (nonatomic, strong) UIButton*     laheiButton;
@property (nonatomic, strong) UIButton*     closeButton;

@property (nonatomic,strong) WTUserInfoService*     userInfoService;

@end

@implementation WTPersonInfoView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setup];
    }
    return self;
}


#pragma mark - 
- (void)setup{
    
    self.contentView = ({
        UIView* view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.cornerRadius = 8;
        view.layer.masksToBounds = YES;
        [self addSubview:view];
        view;
    });

    
    self.iconImageView = ({
        UIImageView* view = [[UIImageView alloc] init];
        view.layer.cornerRadius = 32;
        view.layer.masksToBounds = YES;
        view.image = [UIImage wt_imageWithName:@"wt_avatar"];
        [self addSubview:view];
        view;
    });
    
    self.personInfoNickView = ({
        
        WTPersonInfoNickView* view = [[WTPersonInfoNickView alloc] init];
        [self.contentView addSubview:view];
        view;
    });
    
    self.idLabel = ({
        UILabel* label = [[UILabel alloc] init];
        label.textColor = [UIColor hexChangeFloat:@"969696"];
        label.font = [UIFont systemFontOfSize:9];
        label.text = @"账号:保密";
        [self.contentView addSubview:label];
        label;
    });
    
    
    self.addressView = ({
        WTAddressView* view = [[WTAddressView alloc] init];
        [self.contentView addSubview:view];
        view;
    });
    
    self.desLabel = ({
        UILabel* label = [[UILabel alloc] init];
        label.numberOfLines = 1;
        label.textColor = [UIColor hexChangeFloat:@"969696"];
        label.font = [UIFont systemFontOfSize:11];
        label.text = @"个性签名:这个家伙很懒，什么也没写";
        [self.contentView addSubview:label];
        label;
    });
    
    self.toolView = ({
        WTPersonToolView* view = [[WTPersonToolView alloc] init];
        [self.contentView addSubview:view];
        view;
    });
    
    self.socialView = ({
        WTPersonSocialView* view = [[[NSBundle bundleForClass:[self class]] loadNibNamed:@"WTPersonSocialView" owner:self options:nil] lastObject];
        [self.contentView addSubview:view];
        view;
    });
    
    self.jubaoButton = ({
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage wt_imageWithName:@"wt_person_jubao_icon"] forState:UIControlStateNormal];
        [button setTitle:@"举报" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor hexChangeFloat:@"969696"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:8];
        [self.contentView addSubview:button];
        button;
    });
    
    self.closeButton = ({
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage wt_imageWithName:@"wt_person_close_icon"] forState:UIControlStateNormal];
        [self.contentView addSubview:button];
        button;
    });

    [self layout];
}


#pragma mark - 
- (void)layout{
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(25);
        make.trailing.mas_equalTo(-15);
        make.leading.mas_equalTo(15);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.mas_equalTo(65);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(0);
    }];
    
    [self.personInfoNickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(15);
        make.width.mas_greaterThanOrEqualTo(135);
    }];
    
    [self.idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(self.personInfoNickView.mas_bottom).offset(15);
    }];
    
    [self.addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.idLabel.mas_bottom).offset(10);
        make.width.mas_greaterThanOrEqualTo(40);
        make.centerX.mas_equalTo(self.mas_centerX);
    }];
    
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(self.addressView.mas_bottom).offset(10);
        make.width.mas_lessThanOrEqualTo(200);
    }];
    
    
    [self.socialView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(self.toolView.mas_top).offset(-10);
        make.height.mas_equalTo(50);
    }];
    
    [self.toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.and.bottom.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    
    [self.jubaoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.leading.mas_equalTo(15);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(30);
    }];
    

    
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
        make.trailing.mas_equalTo(-10);
    }];
    
    
    self.jubaoButton.lx_layoutType = LXButtonLayoutTypeImageTop;
    self.jubaoButton.lx_subMargin = 4;
    
    [self addActions];
}


#pragma mark - 
- (void)buttonPressed:(id)sender{
    
    WTPersonInfoType type = 0;
    if(sender == self.jubaoButton){
        type = WTPersonInfoJuBao;
    }else if(sender == self.laheiButton){
        type = WTPersonInfoLaHei;
    }else if(sender == self.closeButton){
        type = WTPersonInfoClose;
    }
    
    if(self.block){
        self.block(type);
    }
}


- (void)addActions{
    
    __block WTPersonInfoType type = 0;
    __weak __typeof(self)weakSelf = self;
    self.toolView.block = ^(NSInteger index){
        if(index == 0){
            type = WTPersonInfoPrivateMessageAction;
        }else if(index == 1){
            type = WTPersonInfoAttentionAction;
        }
        if(weakSelf.block){
            weakSelf.block(type);
        }
    };
}

#pragma mark - 
- (void)setNet_ease_ID:(NSString *)net_ease_ID{

    _net_ease_ID = net_ease_ID;
    __weak __typeof(self)weakSelf = self;
    [self.userInfoService start:@{@"net_ease_or_user_id":net_ease_ID} completionHandler:^(id responseObject, NSError *error) {
        
        if(!error){
            
            WTUserInfoModel* userInfoModel = responseObject;
            [weakSelf.iconImageView sd_setImageWithURL:[NSURL URLWithString:userInfoModel.userPrivateInfoModel.privateinfo_avatar] placeholderImage:[UIImage wt_imageWithName:@"wt_avatar"]];
            
            if([userInfoModel.userPrivateInfoModel.privateinfo_cutecode longLongValue] > 0){
                [weakSelf.idLabel setText:[NSString stringWithFormat:@"%@%@",@"账号:",userInfoModel.userPrivateInfoModel.privateinfo_cutecode]];
            }
            weakSelf.personInfoNickView.levelLabel.text = [NSString stringWithFormat:@"%zd",[userInfoModel.userRelationInfoModel.relationinfo_level longLongValue]];
            weakSelf.personInfoNickView.nickLabel.text = userInfoModel.userPrivateInfoModel.privateinfo_name;
            
            if([userInfoModel.userPrivateInfoModel.privateinfo_sex isEqualToString:@"M"]){
                weakSelf.personInfoNickView.sexImageView.image = [UIImage wt_imageWithName:@"wt_person_sex_man_icon"];
            }else {
                weakSelf.personInfoNickView.sexImageView.image = [UIImage wt_imageWithName:@"wt_person_sex_women_icon"];
            }
            
            weakSelf.socialView.view1.numberLabel.text = [NSString coinCount:[NSString stringWithFormat:@"%zd",[userInfoModel.userRelationInfoModel.relationinfo_focus longLongValue]]];
            
            weakSelf.socialView.view2.numberLabel.text = [NSString coinCount:[NSString stringWithFormat:@"%zd",[userInfoModel.userRelationInfoModel.relationinfo_fans longLongValue]]];
            weakSelf.socialView.view3.numberLabel.text = [NSString coinCount:[NSString stringWithFormat:@"%lld",[userInfoModel.userRelationInfoModel.relationinfo_expense longLongValue]]];
            weakSelf.socialView.view4.numberLabel.text = [NSString coinCount:[NSString stringWithFormat:@"%lld",[userInfoModel.userRelationInfoModel.relationinfo_income longLongValue]]];

            if(userInfoModel.userPrivateInfoModel.privateinfo_signature
               && userInfoModel.userPrivateInfoModel.privateinfo_signature.length > 0){
                weakSelf.desLabel.text = [NSString stringWithFormat:@"%@%@",@"个性签名:",userInfoModel.userPrivateInfoModel.privateinfo_signature];
            }
            
            if(userInfoModel.userPrivateInfoModel.privateinfo_address
               && userInfoModel.userPrivateInfoModel.privateinfo_address.length > 0){
                weakSelf.desLabel.text = [NSString stringWithFormat:@"%@",userInfoModel.userPrivateInfoModel.privateinfo_address];
            }
        }
    }];
}



- (void)setIs_focused:(BOOL)is_focused{
    
    _is_focused = is_focused;
    if(is_focused){
        [self.toolView.attentionButton setTitle:@"已关注" forState:UIControlStateNormal];
    }else {
        [self.toolView.attentionButton setTitle:@"关注" forState:UIControlStateNormal];
    }
}


#pragma mark getter
- (WTUserInfoService *)userInfoService{
    if(!_userInfoService){
        _userInfoService = [[WTUserInfoService alloc] init];
    }
    return _userInfoService;
}
@end
